﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dazaza.Domain.Model;
using Dazaza.Domain.Model.SearchInfo;
using Dazaza.Framework.Data;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Caching;

namespace Dazaza.Repositories
{
    /// <summary>
    /// 分类相关
    /// </summary>
    public class CategoryRepository
    {
        private static readonly bool isCache = true;
        private static readonly int cacheMinutes = 600;

        /// <summary>
        /// 获取二级分类（根据classid）
        /// </summary>
        /// <param name="classID"></param>
        /// <returns></returns>
        public static List<CategoryModel> GetCategoryList(string classID)
        {
            string cacheName = string.Format("GetCategoryList{0}",classID);
            if (isCache)
            {
                var obj = MemoryCache.Default.Get(cacheName);
                if ( obj != null)
                {
                    return obj as List<CategoryModel>;
                }
            }

            string sql = "select * from [category] with(nolock) where classid = @classid";
            DataCommand dc = new DataCommand(sql,CommandType.Text, DataBaseType.Main);
            dc.SetParameterValue("@classid", SqlDbType.Int, classID);

            var result = dc.ExecuteEntityList<CategoryModel>();

            if (isCache)
            {
                CacheItemPolicy cip = new CacheItemPolicy();
                cip.SlidingExpiration = new TimeSpan(0, 0, cacheMinutes, 0, 0);
                MemoryCache.Default.Set(new CacheItem(cacheName, result), cip);
            }

            return result;
        }

        /// <summary>
        /// 获取二级分类，根据className
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public static List<CategoryModel> GetCategoryListByClassName(string className)
        {
            string cacheName = string.Format("GetCategoryListByClassName{0}", className);
            if (isCache)
            {
                var obj = MemoryCache.Default.Get(cacheName);
                if (obj != null)
                {
                    return obj as List<CategoryModel>;
                }
            }

            string sql = "select * from [category] with(nolock) where name_en like ''+@classname+'%' ";
            DataCommand dc = new DataCommand(sql, CommandType.Text, DataBaseType.Main);
            dc.SetParameterValue("@classname",SqlDbType.VarChar,className);

            var result = dc.ExecuteEntityList<CategoryModel>();

            if (isCache)
            {
                CacheItemPolicy cip = new CacheItemPolicy();
                cip.SlidingExpiration = new TimeSpan(0, 0, cacheMinutes, 0, 0);
                MemoryCache.Default.Set(new CacheItem(cacheName, result), cip);
            }


            return result;
        }

        /// <summary>
        /// 获取一级分类
        /// </summary>
        /// <returns></returns>
        public static List<CategoryClassModel> GetCategoryClassList()
        {
            string cacheName = "GetCategoryClassList";
            if (isCache)
            {
                var obj = MemoryCache.Default.Get(cacheName);
                if (obj != null)
                {
                    return obj as List<CategoryClassModel>;
                }
            }

            string sql = "select * from [class] with(nolock) ";
            DataCommand dc = new DataCommand(sql, CommandType.Text, DataBaseType.Main);

            var result = dc.ExecuteEntityList<CategoryClassModel>();

            if (isCache)
            {
                CacheItemPolicy cip = new CacheItemPolicy();
                cip.SlidingExpiration = new TimeSpan(0, 0, cacheMinutes, 0, 0);
                MemoryCache.Default.Set(new CacheItem(cacheName, result), cip);
            }

            return result;
        }
    }
}
